
<input type="hidden" id="count_ab" value="0">
<div class="wrapper wrapper-content page-heading animated fadeInRight">
    <div class="row">
        <div class="col-lg-9">
            <div class="tabs-container">
                <ul class="nav nav-tabs" id="tab-list">
                    <li id="search-link" class="active"><a data-toggle="tab" role="tab" href="#search-1">List</a></li>
                </ul>
                <div class="tab-content">
                    <div id="search-1" class="tab-pane fade-in active">
                        <div class="panel-body">
                            <div class="ibox-title" style="border: none;"><h5 style="color: #00afef"><i></i></h5>
                            </div>
                            <button data-toggle="tab" href="#add-2" class="btn btn-success btn-md m-t-n-xs add-tab-btn" type="button" id="add-form-tab">Add</button>
                            <div class="ibox-content">
                                <div class="table-responsive">
                                    <table class="dataTables table table-bordered table-condense table-hover" cellspacing="0" width="100%">
                                        <thead>
                                            <tr>
                                                <th class="no-sort"></th>
                                                <th>Program Name</th>
                                                <th>Run Type</th>
                                                <th>Run By</th>
                                                <th>Output Type</th>
                                                <th>Result</th>
                                                <th>Status</th>
                                                <th>Output Options</th>
                                                <th>Schedule Type</th>
                                                <th>Interval</th>
                                                <th>Output File</th>
                                            </tr>
                                        </thead><!-- 
                                        <tbody>
                                            <?php foreach($data as $d): ?>
                                            <tr>
                                                <td><?php echo $d->program_name; ?></td>
                                                <td><?php echo $d->run_type_m; ?></td>
                                                <td><?php echo $d->user_name; ?></td>
                                                <td><?php echo $d->output_type_m; ?></td>
                                                <td><?php echo $d->result_m; ?></td>
                                                <td><?php echo $d->status_m; ?></td>
                                                <td><?php echo $d->output_option_m; ?></td>
                                                <td><?php echo $d->sched_type_m; ?></td>
                                                <td><?php echo $d->interval; ?></td>
                                                <td><?php echo $d->output_file; ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                        </tbody> -->
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<input type="hidden" id="count_add" value="0">
<input type="hidden" id="count_edit" value="0">
<input type="hidden" id="count_view" value="0">
        <script type="text/javascript">
        $(document).ready(function() {

                    /*$('.dataTables').DataTable( {
                        "bProcessing": false,
                        "sAjaxSource": "your.php",
                        "order": [[ 1, "desc" ]],
                        "responsive" : true,
                        "columnDefs": [ {
                          "targets": 'no-sort',
                          "orderable": false,
                          }],
                          "sDom": 'lftip'
                    });*/
                    var table = $('.dataTables').DataTable( {
                        "ajax": "<?php echo base_url($this->session->userdata('forajax').'/getdatus'); ?>",
                        "columnDefs": [ {
                          "targets": 'no-sort',
                          "orderable": false,
                          }],
                          "sDom": 'lftip'
                    });
                    setInterval( function () {
                        table.ajax.reload(null,false);
                    }, 1000 );

                    var tab = 0;
                    var counter = 1;
                    var counter2 = 1;
                    $('#tab-list').on('click','#close-add-body',function(){
                        var tabId = $(this).parents('li').children('a').attr('href');
                        $(this).parents('li').remove('li');
                        $(tabId).remove();
                        $('#tab-list a:first').tab('show');
                        if(tabId == "#view"){
                            $('#count_view').val('0');
                        }else if(tabId == "#edit"){
                            $('#count_edit').val('0');
                        }else{
                            $('#count_add').val('0');
                        }
                    });
                    $('#ena-v :input(:checkbox)').iCheck('');
                    //$('#loading-icon').hide();

                    $('#search-form').click(function(e){
                        $('#notify').hide();
                        $('#search-form-result').html('');
                        e.preventDefault();
                        $('#notify').hide();
                        if(tab == 1){
                          $('.dataTables').dataTable().fnDestroy();
                        }
                        if($('input[name=exe-file-name-s]').val() === '' && $('input[name=form-code-s]').val() === '' && $('input[name=form-name-s]').val() === '' && $('input[name=module-name]').val() === ''){
                            notifymsg('danger','<b>Error:</b> Some Fields are empty.');
                        }else{
                            $.ajax({
                                type: "POST",
                                url: "<?php echo base_url($this->session->userdata('forajax').'/searchFormData'); ?>",
                                data: $('#search-form-forms').serialize(),
                                dataType: 'json',
                                success: function(e){
                                    if(e == 'No results found.'){
                                        $('#search-form-result').append("<td colspan='5' align='center'><h3>No Result Found</h3></td>");
                                        notifymsg('success','<b>Success</b>: 0 result(s) found.');
                                    }else{
                                        var count = 0;
                                        var trHTML = '';
                                        $.each(e, function(i, item) {
                                            trHTML += '<tr><td id="form-code-e">'+ e[i].program_code +'</td><td id="form-name-e">'+ e[i].program_name +'</td><td id="module-name-e">'+ e[i].module_name +'</td><td id="exe-file-name-e">'+ e[i].exe_file_name +'</td><td align="center"><div style="cursor: default;" class="state icheckbox_square-green '+(e[i].enable_flag == "Y" ? 'checked' : '')+'"></div></td><td align="center"><button type="button" class="btn btn-sm btn-primary btn-xs btn-outline edit-tab" formid="'+ e[i].program_id +'" data-toggle="tab" href="#add-2" title="Edit"><i class="fa fa-pencil-square-o fa-lg"></i></button><button type="button" class="btn btn-outline btn-sm btn-primary btn-xs view-tab" formid="'+ e[i].program_id +'" data-toggle="tab" role="tab" href="#view" title="View"><i class="fa fa-file-text-o fa-lg"></i></button></td></tr>';
                                            count++;
                                        });
                                        $('#search-form-result').html(trHTML);
                                        notifymsg('success','<b>Success</b>: ' +count+' result(s) found.');
                                        tab = 1;
                                        $('.dataTables').DataTable( {
                                            "order": [[ 1, "desc" ]],
                                            "columnDefs": [ {
                                              "targets": 'no-sort',
                                              "orderable": false,
                                              } ],
                                              "sDom": 'ltipf'
                                          });
                                    }
                                },
                                complete: function(data) {
                                    $('#search-form').removeAttr('disabled');
                                },
                                beforeSend: function(){
                                    $('#search-form').attr('disabled',true);
                                }
                            });
                            $('#notify').hide();
                        }
                    });

                     $(document).on('click','.btn-outline.btn.btn-primary.btn-sm.close-add',function(){
                        console.log('close-add is click');
                         swal({
                            title: "Are you sure?",
                            text: "You want to leave this page?",
                            type: "warning",
                            showCancelButton: true,
                            confirmButtonColor: "#DD6B55",
                            confirmButtonText: "Yes, Please.",
                            cancelButtonText: "No, I'll Stay",
                            closeOnConfirm: true,
                            closeOnCancel: true },
                        function (isConfirm) {
                            if (isConfirm) {
                                swal("Come Again", "I will miss you :'(", "success");
                                $('#add-link').remove();
                                $('#add').remove();
                                $('#tab-list a:first').tab('show');
                               $('#count_add').val('0');
                            } else {
                                swal("Staying", "Thank you for staying :)", "info");
                            }
                        });
                    });

                    $(document).on('click','.btn-outline.btn.btn-primary.btn-sm.close-edit',function(){
                        console.log('close-add is click');
                         swal({
                            title: "Are you sure?",
                            text: "You want to leave this page?",
                            type: "warning",
                            showCancelButton: true,
                            confirmButtonColor: "#DD6B55",
                            confirmButtonText: "Yes, Please.",
                            cancelButtonText: "No, I'll Stay",
                            closeOnConfirm: true,
                            closeOnCancel: true },
                        function (isConfirm) {
                            if (isConfirm) {
                                swal("Come Again", "I will miss you :'(", "success");
                                $('#edit-link').remove();
                                $('#edit').remove();
                                $('#tab-list a:first').tab('show');
                                $('#count_edit').val('0');
                            } else {
                                swal("Staying", "Thank you for staying :)", "info");
                            }
                        });
                    });
                    
                    $(document).on('click', '.btn.btn-primary.btn-outline.btn-sm.view-tab', function(){
                        var form_id = $(this).attr('formid');
                        if($('#count_view').val() == 0){
                            $('#tab-list').append('<li id="view-link"><a href="#view" role="tab" data-toggle="tab">View<button id="close-add-body" style="margin-left:10px;"class="close" type="button" title="Remove this page">×</button></a></li>');
                            $('.tab-content').append('<div class="tab-pane" id="view"><div class="panel-body"><div id="view_data"><div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div></div></div></div>');
                            $('#view-link').show();
                            $('#count_view').val('1');
                        }
                        $('#tab-list a[href="#view"]').tab('show');
                        $("#view_data").load("<?php echo base_url($this->session->userdata('forajax').'/view_form/'); ?>/"+ form_id );
                    });
                    
                    $("select #moduleIdentify").change(function(){
                        $('#empty-option').hide();
                     });

                    $(document).on('click', '.btn.btn-primary.btn-outline.btn-sm.edit-tab', function(){
                        var form_id = $(this).attr('formid');
                        if($('#count_edit').val() == 0){
                            $('#tab-list').append('<li id="edit-link"><a href="#edit" role="tab" data-toggle="tab">Edit<button id="close-add-body" style="margin-left:10px;"class="close" type="button" title="Remove this page">×</button></a></li>');
                            $('.tab-content').append('<div class="tab-pane" id="edit"><div class="panel-body"><div id="edit_data"><div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div></div></div></div>');
                            $('#edit-link').show();
                            $('#count_edit').val('1');
                        }
                        $('#tab-list a[href="#edit"]').tab('show');
                        $("#edit_data").load("<?php echo base_url($this->session->userdata('forajax').'/edit_form/');?>/" + form_id );
                    });

                     $('#add-form-tab').click(function(){
                        if($('#count_add').val() == 0){
                            $('#tab-list').append('<li id="add-link"><a href="#add" role="tab" data-toggle="tab">Add<button id="close-add-body" style="margin-left:10px;"class="close" type="button" title="Remove this page">×</button></a></li>');
                            $('.tab-content').append('<div class="tab-pane" id="add"><div class="panel-body"><div id="add_data"><div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div></div></div></div>');
                            $('#add-link').show();
                            $('#count_add').val('1');
                        }
                        $('#tab-list a[href="#add"]').tab('show');
                        $("#add_data").load("<?php echo base_url($this->session->userdata('forajax').'/add_form/'); ?>");
                    });

                    $('#search-btn').click(function(){
                        var searchitem = $('input[name=search-code]').val();
                        $('#msg').html('');
                        $('input[name=search-code]').val().toUpperCase();
                        $.ajax({
                            type: "POST",
                            url: "<?php echo base_url($this->session->userdata('forajax').'/searchCode'); ?>",
                            data: { "form-code": searchitem  },
                            dataType: 'json',
                            success: function(data){
                                if(data == '' ){
                                    $('#msg').html('No Results found');
                                }else{
                                    $('#msg').html('<i style="color:green;" class="fa fa-check"></i>');
                                }
                                var temp = '';
                               $.each(data, function(i, item) {
                                    $('#menu-names').html('');
                                    temp += '<option value='+ data[i].menu_id +'>' + data[i].menu_name + '</option>';
                                    $('#f-name').val(data[i].form_name);
                                    $('#f-module').val(data[i].module_name);
                                    $('#f-id').val(data[i].form_id);
                                });
                               $('#menu-names').append(temp);
                            },
                            beforeSend: function(){
                                $('#search-btn').prop('disabled', true);
                                $('#loading-icon').addClass('fa-spin');
                            },
                            complete: function(){
                                $('#search-btn').prop('disabled', false);
                                $('#loading-icon').removeClass('fa-spin');
                            }
                        });
                    });
        });
        </script>